import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';
import render from '@feidao/web/atom/nodejs/render/render';

import get_menus from '@feidao/web/atom/nodejs/authorization/get-menus';
// import get_params from '@feidao/web/atom/nodejs/authorization/get-params';

import storage_set from '@feidao/web/atom/nodejs/storage/put';
import P03 from './P03';

function render_data(data: any[], p_name: string, is_open: boolean, is_close: boolean) {
	let res = '';
	function get_first_a(menu_name: string, package_url: string, icon: string) {
		let h;
		if (is_close) {
			h = ' href="' + package_url + '?left=%22close%22"';
		} else {
			h = ' href="' + package_url + '"';
		}
		if (!package_url || package_url === 'undefined' || package_url === 'null') {
			h = ' style="cursor:default"';
		}
		if (is_open) {
			return '<li><a class="display-b" style="padding-left:25px;height:50px;line-height:50px;"' + h + '><i style="padding-right:10px;" class="iconfont font20 vertical-a-m ' + icon + '"></i><span class="font14">' + menu_name + '</span></a>';
		} else {
			return '<li><a class="display-b" style="padding-left:25px;height:50px;line-height:50px;"' + h + '><i style="padding-right:10px;" class="iconfont font20 vertical-a-m ' + icon + '"></i></a>';
		}
	}
	function get_li(menu_name: string, package_url: string) {
		let h;
		if (is_close) {
			h = ' href="' + package_url + '?left=%22close%22"';
		} else {
			h = ' href="' + package_url + '"';
		}
		if (!package_url || package_url === 'undefined' || package_url === 'null') {
			h = ' style="cursor:default"';
		}
		return '<li><a class="display-b font12 tiaozhuan" style="padding-left:25px;height:50px;line-height:50px;"' + h + '>' + menu_name + '</a>';
	}
	let p_m = '';
	const pas = {};
	const check = {};
	const len = {};
	select_data("");
	if (pas[p_m] == 0) {
		res = res + '</li>';
	} else {
		res = res + '</ul></li>';
	}

	function select_data(parent_no: string) {
		data.forEach((item) => {
			const menu_no = item.menu_no;
			const _parent_no = item.parent_no;
			const menu_name = item.menu_name;
			const package_url = item.package_url;
			const icon = item.icon;
			if (parent_no == _parent_no) {
				const p_f_d = filter_data(menu_no);
				if (parent_no == "") {
					if (pas[p_m]) {
						if (pas[p_m] == 0) {
							res = res + '</li>';
						} else {
							res = res + '</ul></li>';
						}
					} else {
						pas[menu_no] = p_f_d.length;
					}
					p_m = menu_no;
					res = res + get_first_a(menu_name, package_url, icon);
				} else {
					if (!check[_parent_no]) {
						check[_parent_no] = 1;
					} else {
						check[_parent_no] = check[_parent_no] + 1;
					}
					res = res + get_li(menu_name, package_url);
				}
				if (p_f_d.length > 0) {
					if (is_open) {
						res = res + '<ul>';
					} else {
						res = res + '<ul class="left_69" style="left:70px">';
					}
				} else {
					res = res + '</li>';
					len[_parent_no] = filter_data(_parent_no).length;
					if (len[_parent_no] == check[_parent_no]) {
						res = res + '</ul></li>';
					}
				};
				select_data(menu_no);
			}
		});
	}

	function filter_data(p_no: string) {
		const filter = data.filter((item) => {
			const _parent_no = item.parent_no;
			return _parent_no == p_no;
		})
		return filter;
	}
	const obj = {};
	obj[p_name] = res;
	return obj;
}

export default async function na001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	const menus = await get_menus(fd.data.msg, fd.data.headers);
	console.warn(menus);

	await storage_set('menus', menus, "tourism-serve");

	if (fd.data.params['left']) {
		render(fd.data.node, { width: "70px", close: true }, P03, 'P03');
		render(fd.data.node, menus, render_data(menus, 'P01', true, true)['P01'], 'P01');
		render(fd.data.node, menus, render_data(menus, 'P02', false, true)['P02'], 'P02');
	} else {
		render(fd.data.node, { width: "165px", close: false }, P03, 'P03');
		render(fd.data.node, menus, render_data(menus, 'P01', true, false)['P01'], 'P01');
		render(fd.data.node, menus, render_data(menus, 'P02', false, false)['P02'], 'P02');
	}


}